

package com.sdry.service.impl.llm;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.sdry.mapper.llm.DepartmentMapper;
import com.sdry.mapper.llm.EmployeeMapper;
import com.sdry.mapper.llm.TeamMapper;
import com.sdry.mapper.llm.UserMapper;
import com.sdry.model.llm.Department;
import com.sdry.model.llm.Employee;
import com.sdry.model.llm.Team;
import com.sdry.model.llm.User;
import com.sdry.service.llm.EmployeeService;

/** 
 * 员工管理
* @author lian 
* @version 1.0
* @time 2018年10月17日 上午12:54:04
*/

@Service
public class EmployeeServiceImp implements EmployeeService{

	@Resource
	private EmployeeMapper empMapper;  //注入员工mapper
	@Resource
	private UserMapper userMapper;  //注入用户mapper
	@Resource
	private TeamMapper teamMapper;  //注入班组mapper
	@Resource
	private DepartmentMapper depMapper;  //注入部门mapper
	
	/**
	 * 查询员工数据总行数
	 * @param emp 员工
	 * @return
	 */
	@Override
	public int countEmp(Employee emp) {
		return empMapper.countEmp(emp);
	}

	/**
	 * 分页查询员工
	 * @param map 参数集合
	 * @return
	 */
	@Override
	public List<Employee> listPageEmp(Map<String, Object> map) {
		List<Employee>  list = empMapper.listPageEmp(map);
		for(Employee emp : list){
			if(null != emp.getUid()){
				User user = userMapper.findUserByUid(emp.getUid());
				emp.setUserName(user.getUserName());
				emp.setAccount(user.getAccount());
				Department dep = depMapper.getDepartmentByDid(Long.parseLong(user.getDepName()));
				emp.setDepartmentName(dep.getName());
			}
			if(null != emp.getTid()){
				Team team = teamMapper.getTeamByTid(emp.getTid());
				if(null != team){
					emp.setTeamName(team.getTname());
				}
			}
		}
		return list;
	}


	/**
	 * 添加员工
	 * @param emp 员工
	 * @return
	 */
	@Override
	public int insertEmp(Employee emp) {
		int isSuccess = -2;
		if(null != emp.getUid() && !"".equals(emp.getUid())){
			//如果已经添加过该员工就不能再添加
			List<Employee> empList = empMapper.findEmployeeByUid(emp.getUid());
			if(empList.size() == 0){
				int i = empMapper.insertEmp(emp);
				if(i > 0){
					isSuccess = 2;
				}
			}
		}
		return isSuccess;
	}

	/**
	 * 修改员工
	 * @param emp 员工
	 * @return
	 */
	@Override
	public int updateEmp(Employee emp) {
		int isSuccess = -3;
		if(null != emp.getUid() && !"".equals(emp.getUid())){
			//如果已经添加过该员工就不能再添加
			List<Employee> empList = empMapper.findEmployeeByUid(emp.getUid());
			if(empList.size() == 0){
				int i = empMapper.updateEmp(emp);
				if(i > 0){
					isSuccess = 3;
				}
			}
		}
		return isSuccess;
	}

	/**
	 * 删除员工
	 * @param str 员工ID组成的字符串
	 * @return
	 */
	@Transactional(rollbackFor={Exception.class})
	public int deleteEmp(String str) {
		int isSuccess = 0;
		String[] split = str.split(",");
		try {
			for (String id : split) {
				isSuccess = empMapper.deleteEmp(Long.parseLong(id));
			}
		}catch (
		Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return isSuccess;
	}

	/**
	 * 查询班组人数
	 * @param tid 班组ID
	 * @return
	 */
	@Override
	public int countByTeam(String tname) {
		int num = empMapper.countByTeam(tname);
		if( num > 0){
			return num;
		}else{
			return 0;
		}
		
	}

	/**
	 * 查询班组所有员工
	 * @param tid 班组ID
	 * @return
	 */
	@Override
	public String listByTeam(String tname) {
		List<Employee> list = empMapper.listByTeam(tname);
		//把所有员工的id拼接为一个字符串
		StringBuffer sb = new StringBuffer("");
		for(Employee emp : list){
			sb.append(emp.getUid() + ",");
		}
		String str = "";
		if(sb.length() > 1){
			str = sb.substring(0, sb.length()-1);
		}
		return str;
	}

	/**
	 * 查询所有员工
	 * @return
	 */
	@Override
	public List<User> listAllEmp() {
		return empMapper.listAllEmp();
	}

	/**  
	 * 查询所有班组的员工      
	 * @return    
	 */
	@Override
	public List<User> listAllUserInTeam() {
		return empMapper.listAllUserInTeam();
	}

}
